arrive.js
![CDNJS version](https://img.shields.io/cdnjs/v/arrive.svg)
A lightweight JS library for watching DOM element creation and removal using Mutation Observers.
Key Features
- Watch for element creation and removal in the DOM
- Zero dependencies
- Lightweight implementation
- Flexible configuration options
Installation
npm install arrive --save
Or download arrive.min.js directly.
Usage
Watch for New Elements
document.arrive(".test-elem", function(newElem) {
});
document.querySelector(".container").arrive(".test-elem", function(newElem) {
});
document.querySelectorAll(".box").arrive(".test-elem", function(newElem) {
});
Promise-based Usage
Important: Promise-based syntax resolves only for the first matching element. For monitoring multiple or continuous element creation, use the callback approach described above.
Note: There's no need to use the onceOnly
option with Promise-based usage as Promises inherently resolve only once.
var newElem = await document.arrive(".test-elem");
Options
The arrive
event accepts an optional configuration object:
{
fireOnAttributesModification: false,
onceOnly: false,
existing: false,
timeout: 0
}
Example:
document.arrive(".test-elem", {
fireOnAttributesModification: true,
existing: true,
onceOnly: true,
timeout: 5000
}, (newElem) => {
console.log(newElem);
});
Watch for Elements Removal
Use the leave
event to detect when elements are removed from the DOM.
Important: Due to MutationObserver API limitations, the selector must be direct (e.g., .test-elem
) and cannot use descendant or child combinators (e.g., .page .test-elem
is not allowed).
document.querySelector(".container-1").leave(".test-elem", function(removedElem) {
});
document.querySelector(".container-1").leave(".test-elem", {
onceOnly: true,
timeout: 5000
}, function(removedElem) {
});
Unbinding Event Listeners
For better performance, make sure to remove listeners when they are no longer needed:
document.unbindArrive();
document.querySelector(".box").unbindArrive();
document.unbindArrive(".test-elem");
document.unbindArrive(callbackFunc);
document.unbindArrive(".test-elem", callbackFunc);
Arrive.unbindAllArrive();
document.unbindLeave();
Arrive.unbindAllLeave();
jQuery Support
If you use jQuery, you can call all arrive functions on jQuery elements as well:
$(document).arrive(".test-elem", function(newElem) {
});
$(".container-1").arrive(".test-elem", function(newElem) {
});
Browser Support
arrive.js is built over Mutation Observers which is introduced in DOM4. It's supported in latest versions of all popular browsers.
Browser | Supported Versions |
---|
Google Chrome | 27.0+ |
Firefox | 14.0+ |
Safari | 6.1+ |
Internet Explorer | 11.0+ |
Opera | 14.0+ |
Contributing
Report a bug / Request a feature
If you want to report a bug or request a feature, use the Issues section. Before creating a new issue, search the existing ones to make sure that you're not creating a duplicate. When reporting a bug, be sure to include OS/browser version and steps/code to reproduce the bug, a JSFiddle would be great.
Development
If you want to contribute to arrive, here is the workflow you should use:
- Fork the repository.
- Clone the forked repository locally.
- From the
dev
branch, create and checkout a new feature branch to work upon. (If you want to work on some minor bug fix, you can skip this step and continue to work in dev
branch) - Make your changes in that branch (the actual source file is
/src/arrive.js
). - If sensible, add some jasmine tests in
/tests/spec/arriveSpec.js
file. - Make sure there are no regressions by executing the unit tests by opening the file
/tests/SpecRunner.html
in a browser. There is a button 'Run tests without jQuery' at the top left of th page, click that button to make sure that the tests passes without jQuery. Run the test cases in all major browsers. - Push the changes to your github repository.
- Submit a pull request from your repo back to the original repository.
- Once it is accepted, remember to pull those changes back into your develop branch!
Some features/bugs you can send pull requests for
- #70: Add Typescript types to the project
- #69: Option to watch for text change
Keywords
javascript, js, jquery, node.js, watch, listen, creation, dynamically, removal, new, elements, DOM, dynamic, detect, insertions, event, bind, live, livequery